/*
 * @Author: jiu yin
 * @Date: 2024-05-08 18:26:46
 * @LastEditTime: 2024-06-24 13:49:03
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \studio-web-admin\src\view\liveApp\index.tsx
 * jiu
 */
import { list_application } from "@/api/home";
import { useRoute } from "@/hooks";
import type { ActionType } from "@ant-design/pro-components";
import { ProTable } from "@ant-design/pro-components";
import { ConfigProvider, Image } from "antd";
import { useRef, useState } from "react";
import "./index.scss";
import AddShop from "./component/AddShop";
import {b_to_mb} from "@/utils"

export default function index() {
	const { push } = useRoute();

	//添加
	let [isModalOpen, setIsModalOpen] = useState(false);

	const actionRef = useRef<ActionType>(null);

	const columns = [
		{
			dataIndex: "index",
			valueType: "indexBorder",
			width: 48,
		},
		{
			title: "场景信息",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className="font-700 flex">
					<Image width={84} height={56} className="border-rd-10px" src={item.cover_url} />
					<div className="ml-10px ">
						<h5 className="c-#161940">{item.name}</h5>
						<p className="c-#5C5F7A">{item.describe}</p>
					</div>
				</div>
			),
		},
		{
			title: "互动参数",
			dataIndex: "max_num",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className=" text-14px">
					<p className="c-#9e9fb0">
						最大组队成员：<span className="c-#323557">{item.max_num}人</span>
					</p>
					<p className="c-#9e9fb0">
						时长：<span className="c-#323557">{item.exp_duration}min</span>
					</p>
				</div>
			),
		},

		{
			title: "场景大小",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<p className=" text-14px c-#161940">{b_to_mb(item.size)}MB</p>
			),
		},
		{
			title: "版本",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<div className=" text-14px">
					<p className="c-#9e9fb0">
						最新版本：<span className="c-#323557">{item.version}</span>
					</p>
					<p className="c-#9e9fb0">
						上次部署版本：<span className="c-#323557">{item.deploy_version || "未安装"}</span>
					</p>
				</div>
			),
		},
		{
			title: "部署状态",
			dataIndex: "nickname",
			hideInSearch: true,
			render: (_: any, item: any) => (
				<p className=" text-14px c-#161940">
					已部署 {item.deploy_num}/{item.device_num}
				</p>
			),
		},
		{
			title: "操作",
			valueType: "option",
			key: "option",
			render: (_: any, item: any) => [
				<a key="editable" onClick={() => push("/liveApp/liveApp/detail?application_id=" + item.application_id)}>
					详情
				</a>,
			],
		},
	];


	const reload = () => {
		actionRef.current && actionRef.current.reload();
	};

	return (
		<div className="base_bg_fff">
			<section className="w-240px controlBg1 h-95px  cursor-pointer" onClick={() => setIsModalOpen(true)}>
				<div className="flex justify-center  pt-28px  ">
					<div className="controlIcon1 w-24px h-24px"></div>
					<p className="c-#373B96 text-20px font-700 ml-14px">添加场景应用</p>
				</div>
			</section>

			<section className="ml--24px">
				<ConfigProvider>
					<ProTable
						columns={columns}
						actionRef={actionRef}
						request={async (params, sort, filter) => {
							let { data } = await list_application({
								page: params.current,
								limit: params.pageSize,
								...params,
							});
							data.data.forEach((item: any, index: number) => (item.key = index + 1));
							return {
								data: data.data,
								success: true,
								total: data.total,
							};
						}}
						rowKey="key"
						tableAlertRender={false}
						search={false}
						options={false}
						pagination={{
							pageSize: 10,
							hideOnSinglePage: true,
						}}
						dateFormatter="string"
					/>
				</ConfigProvider>
			</section>

			{isModalOpen && <AddShop isModalOpen={[isModalOpen, setIsModalOpen]} reload={reload}></AddShop>}
		</div>
	);
}
